<template>
  <div>
    <Add :formId="formId" ref="add" @ok="search"></Add>
    <Update ref="update" @ok="search"></Update>
    <ZgNewPage
      ref="page"
      title="公式管理"
      addBtnName="新增公式"
      addBtnIcon="plus-circle"
      :extraHeight="320-(isIfm?90:0)"
      :labelCol="8"
      :conditions="conditions"
      :batchBtns="batchBtns"
      :dataBtns="dataBtns"
      @search="onSearch"
      @add="toAdd"
      @batch="toBatch"
      @admin="toAdmin"
    >
      <template slot="columns">
        <a-table-column title="公式名称" dataIndex="name"></a-table-column>
        <a-table-column :width="120" title="执行方式">
          <template slot-scope="obj">
            <ZgDicHolder parentCode="executeType" :code="obj.cata"></ZgDicHolder>
          </template>
        </a-table-column>
        <a-table-column :width="140" title="公式触发机制">
          <template slot-scope="obj">
            <ZgDicHolder parentCode="formulaTriggerMode" :code="obj.triggerMode"></ZgDicHolder>
          </template>
        </a-table-column>
      </template>
    </ZgNewPage>
  </div>
</template>
<script>
import Add from './AddDialog'
import Update from './UpdateDialog'
export default {
  props: {
    formId: {
      type: String,
      default: ''
    }
  },
  components: {
    Add, Update
  },
  data () {
    return {
      conditions: [
        { code: 'name', compType: '单行文本', label: '公式名称', defaultVal: '' },
        { code: 'executeType', compType: '字典', label: '执行方式', defaultVal: '', extra: { 字典类型: 'executeType' } },
        { code: 'triggerMode', compType: '字典', label: '触发机制', defaultVal: '', extra: { 字典类型: 'formulaTriggerMode' } }
      ],
      batchBtns: [
        { key: 1, label: '批量删除' }
      ],
      dataBtns: [
        { key: 1, name: '删除', type: 'link', icon: 'fa-trash', isShow: () => { return true } },
        { key: 2, name: '修改', type: 'link', icon: 'fa-pencil', isShow: () => { return true } }
      ]
    }
  },
  watch: {
    formId () {
      this.search()
    }
  },
  methods: {
    search () {
      const pageRef = this.$refs.page
      if (pageRef) {
        pageRef.search()
      } else {
        setTimeout(() => {
          this.search()
        }, 50)
      }
    },
    onSearch (param) {
      if (this.formId.length === 0) {
        return
      }
      this.$http.get('/zg-autoform/formula/page',
        Object.assign({ formId: this.formId }, param), data => {
        this.$refs.page.endSearch(
          {
            total: parseInt(data.total),
            list: data.records.map(t => {
              t.key = t.id
              return t
            })
          }
        )
      })
    },
    toAdd () {
      this.$refs.add.open()
    },
    toAdmin (param) {
      if (param.key === 1) {
        this.confirm(
        () => {
          this.$http.post('/zg-autoform/formula/del', [param.obj.id], () => {
            this.search()
          })
        },
        '确定要删除这条数据么')
      } else if (param.key === 2) {
        this.$refs.update.open(param.obj)
      }
    },
    toBatch (opt) {
      if (opt.key === 1) {
        this.$refs.page.checkIds(ids => {
          this.$http.post('/zg-autoform/formula/del', ids, () => {
            this.search()
          })
        }, '确定要批量删除这些数据')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
